/**
 * @example: base64文件流转成pdf文件并下载
 * @param {*} base64 文件流
 * @param {*} name pdf文件名
 * @return {*}
 */
export function base64ToPdfDownload(base64, name) {
  let bstr = window.atob(base64);
  let n = bstr.length;
  let u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  const URL = new Blob([u8arr], { type: "pdf" });
  var reader = new FileReader();
  reader.readAsDataURL(URL);
  reader.onload = function (e) {
    // 兼容IE
    if (window.navigator.msSaveOrOpenBlob) {
      var bstr = atob(e.target.result.split(",")[1]);
      var n = bstr.length;
      var u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      var blob = new Blob([u8arr]);
      window.navigator.msSaveOrOpenBlob(blob, `${name}.pdf`);
    } else {
      const a = document.createElement("a");
      a.download = `${name}.pdf`;
      a.href = e.target.result;
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
    }
  };
}
